<template>
    <div class="loginContainer">
      <img class="logo" src="../assets/logo.png" />
        <van-nav-bar
            left-arrow
            @click-left="onClickLeft"
            :title="title"
            :fixed="true"
        />


        <div class="container">
            <van-cell-group>
                <van-field
                    v-model="username"
                    required
                    clearable
                    label="用户名"
                    right-icon="question-o"
                    placeholder="请输入用户名"
                    @click-right-icon="$toast('question')"
                />
                <van-field
                    v-model="password"
                    type="password"
                    label="密码"
                    placeholder="请输入密码"
                    required
                />

            </van-cell-group>

            <p>登录&nbsp;&nbsp;&nbsp;|&nbsp;&nbsp;&nbsp;<span @click="reg()">注册</span></p>

            <van-button class="btn" size="large" color="#000" @click="login">登录</van-button>
        </div>
    </div>
</template>

<script>
    import api from '../api/api_pro';


    export default {
        name:'login',
        data(){
            return{
                title:'登录',
                username:'',
                password:''
            }
        },
        methods: {
            onClickLeft() {
                this.$router.go(-1);
            },
            login(){

                let params= new FormData();
                params.append("username",this.username);
                params.append("password",this.password);
                api.login(params).then(res=>{


                    if(res.code===200){
                        localStorage.setItem('access-user', JSON.stringify(res));
                        this.$router.push({path: '/home'});
                    }else{
                        alert("用户名或密码错误");
                    }
                })
            },
            reg(){
                this.$router.push('/register')
            }
        }
    }
</script>

<style scoped>
  .loginContainer{
    position: fixed;
    width: 100%;
    height: 100%;
    padding-top: 20%;
    display: flex;
    flex-direction: column;
    align-items: center;
  }
    .container{
        padding-top: 10%;
    }
    .container p{
        font-size: 14px;
        padding-top: 8%;
        padding-left: 35%;
        color: #706e6e;
    }
    .container p span{
        color: #f00;
    }
    .btn{
        margin-top: 10%;
    }
</style>
